/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var ADS_Client = new Class({
    
    initialize: function(){

    },
    
    run: function(){
        var request = new Request.JSON({
            url:    'http://localhost/wp/',
            onSuccess: function(responseJSON, responseText){
                this.build(responseJSON);                
            }.bind(this)
        }).get({'do':'sendScheme'});       
    },
    
    build: function(scheme){
        //console.log(scheme);
        //load layouts
        var layoutFrameController = new LayoutFrameController();       
        Array.each(scheme.frames.layouts, function(nodeFrame, key){
            var layout = new LayoutFrame({
                'id'  : nodeFrame.id,
                'url' : nodeFrame.url
            });
            layoutFrameController.addFrame(layout);
        });
        //load data sources
        var dataSourceController = new DataSourceController();
        Array.each(scheme.datasources, function(nodeSource, key){
            var dataSource = new DataSource({
                'id'      : nodeSource.id,
                'content' : nodeSource.content
            });
            //add items to datasource
            Array.each(nodeSource.timeline, function(nodeItem, key){
                var item = new Item({
                    'url'       : nodeItem.url,
                    'type'      : nodeSource.content,
                    'duration'  : nodeItem.duration,
                    'validFrom' : nodeItem.validFrom,
                    'validTo'   : nodeItem.validTo
                });
                dataSource.addItem(item);
            });
            dataSourceController.addSource(dataSource);
        });
        //load data frames
        var dataFrameController = new DataFrameController();
        Array.each(scheme.frames.data, function(nodeFrame, key){
            var dataFrame = new DataFrame({
                'id'     : nodeFrame.id,
                'htmlId' : nodeFrame.htmlId
            });
            //add items to data frame
            Array.each(nodeFrame.timeline, function(nodeItem, key){
                //console.log(nodeFrame.htmlId+'-'+nodeItem.id);
                dataFrame.addSource(dataSourceController.find(nodeItem.id));
            });
            dataFrameController.addFrame(dataFrame);
        });
        //start presentation
        var presentation = new PresentationController({
            'lfc' : layoutFrameController,
            'dfc' : dataFrameController
        });
        presentation.start();
        
    }
    
});